package com.neu.wms.util;

import cn.hutool.http.HttpStatus;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @description 统一返回结果类
 * @author : fengSir
 * @date : 16:08 2021/11/12
 */
@Data
public class ResultVo {

    //是否成功
    private Boolean success;

    //返回码
    private Integer code;

    //返回消息
    private String message;

    //返回数据
    private Map<String, Object> data = new HashMap<String, Object>();

    //把构造方法私有
    private ResultVo() {}

    //成功静态方法
    public static ResultVo ok() {
        ResultVo resultVo = new ResultVo();
        resultVo.setSuccess(true);
        resultVo.setCode(HttpStatus.HTTP_OK);
        resultVo.setMessage("成功");
        return resultVo;
    }
    //成功：自己指定消息
    public static ResultVo ok(String msg) {
        ResultVo resultVo = new ResultVo();
        resultVo.setSuccess(true);
        resultVo.setCode(HttpStatus.HTTP_OK);
        resultVo.setMessage(msg);
        return resultVo;
    }

    //成功：自己指定消息和返回code
    public static ResultVo ok(Integer code, String msg) {
        ResultVo resultVo = new ResultVo();
        resultVo.setSuccess(true);
        resultVo.setCode(code);
        resultVo.setMessage(msg);
        return resultVo;
    }

    //失败静态方法
    public static ResultVo error() {
        ResultVo resultVo = new ResultVo();
        resultVo.setSuccess(false);
        resultVo.setCode(HttpStatus.HTTP_BAD_REQUEST);
        resultVo.setMessage("失败");
        return resultVo;
    }
    //失败:自己指定失败消息
    public static ResultVo error(String msg) {
        ResultVo resultVo = new ResultVo();
        resultVo.setSuccess(false);
        resultVo.setCode(HttpStatus.HTTP_BAD_REQUEST);
        resultVo.setMessage(msg);
        return resultVo;
    }

    //失败：自己指定失败消息和code
    public static ResultVo error(Integer code , String msg) {
        ResultVo resultVo = new ResultVo();
        resultVo.setSuccess(false);
        resultVo.setCode(code);
        resultVo.setMessage(msg);
        return resultVo;
    }


    public static ResultVo error(ServerCode serverCode) {
        ResultVo resultVo = new ResultVo();
        resultVo.setSuccess(false);
        resultVo.setCode(serverCode.getCode());
        resultVo.setMessage(serverCode.getMessage());
        return resultVo;
    }

    //给返回消息添加数据
    public ResultVo data(String key, Object value){
        this.data.put(key, value);
        return this;
    }

    //以下四个方法可以不关注，上面的方法已经基本满足需求
    public ResultVo success(Boolean success){
        this.setSuccess(success);
        return this;
    }

    public ResultVo message(String message){
        this.setMessage(message);
        return this;
    }

    public ResultVo code(Integer code){
        this.setCode(code);
        return this;
    }

    public ResultVo data(Map<String, Object> map){
        this.setData(map);
        return this;
    }
}

